package com.evget.loggingwellconstructiondesign.meta.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import ${package}.${moduleName}.mapper.${ClassName}Mapper;
import ${package}.${moduleName}.meta.entity.pojo.${ClassName};
import ${package}.${moduleName}.meta.entity.request.${ClassName}Request;
import ${package}.${moduleName}.meta.entity.request.${ClassName}SelectPageRequest;
import ${package}.${moduleName}.meta.entity.request.${ClassName}SelectRequest;
import ${package}.${moduleName}.meta.entity.response.${ClassName}Response;
import ${package}.${moduleName}.meta.service.${ClassName}Service;
import ${package}.${moduleName}.util.ValidateUtil;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
*
* ${tableComment} 实现类
*
* @author ${author}
* @since  ${date}
*/
@Service
public class ${ClassName}ServiceImpl extends ServiceImpl<${ClassName}Mapper, ${ClassName}>
        implements ${ClassName}Service {

    @Resource
    private ${ClassName}Mapper ${className}Mapper;

    @Override
    public boolean add(${ClassName}Request request) {
        // 参数必填项校验
        ValidateUtil.validateRequest(request);
        // 存在性校验
        ValidateUtil.validateExists(request.get${ClassName}Id(), ${className}Mapper, GlobalErrorCodeConstants.EVGET_ADD_ERROR);
        ${ClassName} ${className} = new ${ClassName}();
        BeanUtil.copyProperties(request, ${className});
        return save(${className});
    }

    @Override
    public boolean addBatch(List<${ClassName}Request> requestList) {
        // 参数必填项校验
        ValidateUtil.validateRequest(requestList);
        // 存在性校验
        ValidateUtil.validateExists(requestList.stream().map(${ClassName}Request:: get${ClassName}Id).collect(Collectors.toList()),
                ${className}Mapper, GlobalErrorCodeConstants.EVGET_ADD_ERROR);
        List<${ClassName}> ${className}s = BeanUtil.copyToList(requestList, ${ClassName}.class);
        return saveBatch(${className}s);
    }

    @Override
    public boolean deleteByIds(List<String> ids) {
        // 参数必填项校验
        if (ids == null || ids.isEmpty()) {
            throw new ServiceException(GlobalErrorCodeConstants.EVGET_REQUETPARM_ERROR.getCode(),
                    GlobalErrorCodeConstants.EVGET_REQUETPARM_ERROR.getMsg());
        }
        // 存在性校验
        ValidateUtil.validateExists(ids, ${className}Mapper, GlobalErrorCodeConstants.EVGET_DELETE_ERROR);
        return removeBatchByIds(ids);
    }

    @Override
    public boolean updateInfo(${ClassName}Request request) {
        // 参数必填项校验
        ValidateUtil.validateRequest(request);
        // 存在性校验
        ValidateUtil.validateExists(request.get${ClassName}Id(), ${className}Mapper, GlobalErrorCodeConstants.EVGET_UPDATE_ERROR);
        ${ClassName} ${className} = new ${ClassName}();
        BeanUtil.copyProperties(request, ${className});
        return ${className}Mapper.updateById(${className}) > 0;
    }

    @Override
    public boolean updateBatch(List<${ClassName}Request> requestList) {
        // 参数必填项校验
        ValidateUtil.validateRequest(requestList);
        // 存在性校验
        ValidateUtil.validateExists(requestList.stream().map(${ClassName}Request:: get${ClassName}Id).collect(Collectors.toList()),
                ${className}Mapper, GlobalErrorCodeConstants.EVGET_UPDATE_ERROR);
        List<${ClassName}> ${className}s = BeanUtil.copyToList(requestList, ${ClassName}.class);
        return updateBatchById(${className}s);
    }

    @Override
    public List<${ClassName}Response> getListByIds(String ids) {
        // 参数必填项校验
        ValidateUtil.validateRequest(ids);
        List<${ClassName}> ${className}s = ${className}Mapper.selectBatchIds(Arrays.stream(ids.split(",")).toList());
        return BeanUtil.copyToList(${className}s, ${ClassName}Response.class);
    }

    @Override
    public List<${ClassName}Response> getList(${ClassName}SelectRequest request) {
        LambdaQueryWrapper<${ClassName}> wrapper = Wrappers.lambdaQuery();
                    <#list queryList as field>
                        <#if field.queryFormType == 'date' || field.queryFormType == 'datetime'>
        wrapper.between(ArrayUtils.isNotEmpty(request.get${field.attrName?cap_first}()), ${ClassName}::get${field.attrName?cap_first}, ArrayUtils.isNotEmpty(request.get${field.attrName?cap_first}()) ? request.get${field.attrName?cap_first}()[0] : null, ArrayUtils.isNotEmpty(query.get${field.attrName?cap_first}()) ? query.get${field.attrName?cap_first}()[1] : null);
                        <#elseif field.queryType == '='>
        wrapper.eq(StringUtils.isNotEmpty(request.get${field.attrName?cap_first}()), ${ClassName}::get${field.attrName?cap_first}, request.get${field.attrName?cap_first}());
                        <#elseif field.queryType == '!='>
        wrapper.ne(StringUtils.isNotEmpty(request.get${field.attrName?cap_first}()), ${ClassName}::get${field.attrName?cap_first}, request.get${field.attrName?cap_first}());
                        <#elseif field.queryType == '>'>
        wrapper.gt(StringUtils.isNotEmpty(request.get${field.attrName?cap_first}()), ${ClassName}::get${field.attrName?cap_first}, request.get${field.attrName?cap_first}());
                        <#elseif field.queryType == '>='>
        wrapper.ge(StringUtils.isNotEmpty(request.get${field.attrName?cap_first}()), ${ClassName}::get${field.attrName?cap_first}, request.get${field.attrName?cap_first}());
                        <#elseif field.queryType == '<'>
        wrapper.lt(StringUtils.isNotEmpty(request.get${field.attrName?cap_first}()), ${ClassName}::get${field.attrName?cap_first}, request.get${field.attrName?cap_first}());
                        <#elseif field.queryType == '<='>
        wrapper.le(StringUtils.isNotEmpty(request.get${field.attrName?cap_first}()), ${ClassName}::get${field.attrName?cap_first}, request.get${field.attrName?cap_first}());
                        <#elseif field.queryType == 'like'>
        wrapper.like(StringUtils.isNotEmpty(request.get${field.attrName?cap_first}()), ${ClassName}::get${field.attrName?cap_first}, request.get${field.attrName?cap_first}());
                        <#elseif field.queryType == 'left like'>
        wrapper.likeLeft(StringUtils.isNotEmpty(request.get${field.attrName?cap_first}()), ${ClassName}::get${field.attrName?cap_first}, request.get${field.attrName?cap_first}());
                        <#elseif field.queryType == 'right like'>
        wrapper.likeRight(StringUtils.isNotEmpty(request.get${field.attrName?cap_first}()), ${ClassName}::get${field.attrName?cap_first}, request.get${field.attrName?cap_first}());
                        </#if>
                    </#list>

        List<${ClassName}> ${className}s = ${className}Mapper.selectList(wrapper);
        return BeanUtil.copyToList(${className}s, ${ClassName}Response.class);
    }

    @Override
    public PageResult<${ClassName}Response> get${ClassName}Page(${ClassName}SelectPageRequest request) {
        LambdaQueryWrapper<${ClassName}> wrapper = Wrappers.lambdaQuery();
                    <#list queryList as field>
                        <#if field.queryFormType == 'date' || field.queryFormType == 'datetime'>
        wrapper.between(ArrayUtils.isNotEmpty(request.get${field.attrName?cap_first}()), ${ClassName}::get${field.attrName?cap_first}, ArrayUtils.isNotEmpty(request.get${field.attrName?cap_first}()) ? request.get${field.attrName?cap_first}()[0] : null, ArrayUtils.isNotEmpty(query.get${field.attrName?cap_first}()) ? query.get${field.attrName?cap_first}()[1] : null);
                        <#elseif field.queryType == '='>
        wrapper.eq(StringUtils.isNotEmpty(request.get${field.attrName?cap_first}()), ${ClassName}::get${field.attrName?cap_first}, request.get${field.attrName?cap_first}());
                        <#elseif field.queryType == '!='>
        wrapper.ne(StringUtils.isNotEmpty(request.get${field.attrName?cap_first}()), ${ClassName}::get${field.attrName?cap_first}, request.get${field.attrName?cap_first}());
                        <#elseif field.queryType == '>'>
        wrapper.gt(StringUtils.isNotEmpty(request.get${field.attrName?cap_first}()), ${ClassName}::get${field.attrName?cap_first}, request.get${field.attrName?cap_first}());
                        <#elseif field.queryType == '>='>
        wrapper.ge(StringUtils.isNotEmpty(request.get${field.attrName?cap_first}()), ${ClassName}::get${field.attrName?cap_first}, request.get${field.attrName?cap_first}());
                        <#elseif field.queryType == '<'>
        wrapper.lt(StringUtils.isNotEmpty(request.get${field.attrName?cap_first}()), ${ClassName}::get${field.attrName?cap_first}, request.get${field.attrName?cap_first}());
                        <#elseif field.queryType == '<='>
        wrapper.le(StringUtils.isNotEmpty(request.get${field.attrName?cap_first}()), ${ClassName}::get${field.attrName?cap_first}, request.get${field.attrName?cap_first}());
                        <#elseif field.queryType == 'like'>
        wrapper.like(StringUtils.isNotEmpty(request.get${field.attrName?cap_first}()), ${ClassName}::get${field.attrName?cap_first}, request.get${field.attrName?cap_first}());
                        <#elseif field.queryType == 'left like'>
        wrapper.likeLeft(StringUtils.isNotEmpty(request.get${field.attrName?cap_first}()), ${ClassName}::get${field.attrName?cap_first}, request.get${field.attrName?cap_first}());
                        <#elseif field.queryType == 'right like'>
        wrapper.likeRight(StringUtils.isNotEmpty(request.get${field.attrName?cap_first}()), ${ClassName}::get${field.attrName?cap_first}, request.get${field.attrName?cap_first}());
                        </#if>
                    </#list>

        return ValidateUtil.getPage(${className}Mapper.selectEvgetPage(request, null, wrapper));
    }
}
